import { Result } from './../../../../model/Result.d';
import { Component, Injector } from '@angular/core';
import { IonicPage} from 'ionic-angular';
import { BasePage } from '../../../basePage';
import { StorageService } from './../../../../providers/StorageService';

import {  nailerService } from './../../../../providers/nailerService';

/**
 * Generated class for the WithdrawPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-withdraw',
  templateUrl: 'withdraw.html',
})
export class WithdrawPage extends BasePage {
  //传过来的用户操作类型
   user_type:any
    userinfo = {};
  //总余额
  old=0;
  //提现金额
  allMoney="";
  //支付宝账号
  alipay="";
  //控制器
  already:boolean=true;
  kflag:boolean=false;

  msg:any;

  //经过验证后正确的金额和支付宝账号
  correctMoney=0;
  correctAlipay="";

  //向后台发送数据
  param={
    withdraw_sum:0,
    withdraw_account:'',
    withdraw_type:0,
    payee_type:0,
    user_id:0
  }
  constructor(injector: Injector,public stc: StorageService,public nailerService:nailerService) {
    super(injector)
    console.log('total', this.navParams.get('total'))
    //传过来的总余额
    this.old =  this.navParams.get('total');
    this.user_type =  this.navParams.get('style');
    console.log(this.user_type);
    this.userinfo = this.stc.read('userinfo');
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad WithdrawPage');
  }

  dismiss(){
    this.viewCtrl.dismiss();
  }
  //点击确认
  confirm(){
    //
    let rePhone=/^1[3|4|5|6|7|8|9]\d{9}$/
    let reEmail=/^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,6}){1,3}$/;
    var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
    if(this.allMoney=="" || this.allMoney == "0" || this.allMoney == "0.0" ||this.allMoney == "0.00"){
      this.presentToast("提现金额不能为空！");
    }else if(this.alipay==""){
      this.presentToast("支付宝账号不能为空！");
    }else if(!rePhone.test(this.alipay) && !reEmail.test(this.alipay)){
      this.presentToast("支付宝账号格式不正确！");
    }  else if(!reg.test(this.allMoney)){
      this.presentToast("提现金额格式不正确，请重新输入");
    }else if(Number(this.allMoney.trim())>this.old){
      this.presentToast("提现金额不能超过余额");
    }else{
       this.correctMoney=Number(this.allMoney);
       this.correctAlipay=this.alipay;
       this.already=false;
    }
    // if(this.allMoney == ""){
    //   //金额不能为空
    //   this.presentToast("请输入提现金额！");
    // }else if(this.alipay==""){
    //   //支付宝账号不能为空
    //   this.presentToast("请输入支付宝账号！");
    // }else if((this.allMoney.charAt(0)=="0" && this.allMoney.charAt(1)!=".")|| this.allMoney.substring(this.allMoney.trim().indexOf(".")+1) == ""){
    //   //不能为0，第一位不能是小数，小数点后不能是空格
    //   this.presentToast("金额格式不正确！");
    // }else if(Number(this.allMoney.trim())>this.old){
    //   //this.showAlert("金额超过可提现余额")
    //   this.presentToast("金额超过可提现余额！");
    // }else if (!re.test(this.alipay)){
    //   this.presentToast("支付宝账号格式不正确！");
    // }else{
    //   this.correctMoney=Number(this.allMoney);
    //   this.correctAlipay=Number(this.alipay);
    //   this.already=false;
    // }
  }
  //退一级，回到填写金额
  back(){
    this.already=true;
  }

  
  // //控制键盘可以按下的键
  // check(e){
  //     var k = e.keyCode;   //48-57是大键盘的数字键，96-105是小键盘的数字键，8是退格符←
  //     console.log(k)
  //     if ((k <= 57 && k >= 48) || (k <= 105 && k >= 96) || (k== 8) ||(k == 46)){
  //       return true;
  //     } else {
  //       return false;
  //     }
  // }

  // //控制输入的数字和小数点的格式
  // only(){
  //   //限制第一个字符必须是数字
  //   this.allMoney = this.allMoney.replace(/[^\d.]/g,"");
  //   //限制只有一个点
  //   this.allMoney =this.allMoney.replace(/^\./g,"");
  //   //限制小数点后2位
  //   this.allMoney=this.allMoney.replace(/\.{2,}/g,".");
  //   this.allMoney = this.allMoney.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
  //   this.allMoney = this.allMoney.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');//只能输入两个小数
  //   this.kflag=false;
  // }

  async withdraw(){
    this.param.withdraw_sum=this.correctMoney;
    this.param.withdraw_account=this.correctAlipay;
    this.param.withdraw_type = 1;             //提现类型 zhi
    //这个不确定
    this.param.payee_type=this.user_type;     //身份类型
    this.param.user_id=this.userinfo['user_id'];
    console.log(this.param)
    let result=await this.nailerService.withDrawAPI(this.param);
    console.log(result);
    this.presentToast(result.msg)
    setTimeout(()=>{
      this.dismiss();
    },2000)
  }
  presentToast(e) {
    let toast = this.toastCtrl.create({
      message: e,
      duration: 2000,
      position: 'bottom'
    });
    toast.onDidDismiss(() => {
      console.log('Dismissed toast');
    });
  
    toast.present();
  }


}
